package cn.lin.service.impl;

import cn.lin.domain.Sign;
import cn.lin.domain.SignUser;
import cn.lin.domain.User;
import cn.lin.handler.SignInWebSocketHandler;
import cn.lin.mapper.SignMapper;
import cn.lin.service.SignService;
import cn.lin.util.KeyWords;
import cn.lin.util.SignUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author cn.Lin
 * @create 2024-12-04-15:56
 */
@Service
@Transactional
public class SignServiceImpl implements SignService {

    @Autowired
    private SignMapper signMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SignInWebSocketHandler signInWebSocketHandler;

    @Override
    public List<SignUser> initiateSignIn(Sign sign) {
        sign.setSignCode(SignUtil.getSignCode());
        int ret = signMapper.initiateSignIn(sign);
        int signId = sign.getId();
        int teacherId = sign.getTeacherId();

        if (ret != 0) {
            String keys = KeyWords.CLASS_SIGN_IN_USER + "List" + signId;
            // 先从redis中查看是否对应班级用户列表
            List<User> classUserList = (List<User>) redisTemplate.opsForValue().get(keys);

            if (classUserList == null) {
                // 查询出对应班级所存在的学生有哪些
                classUserList = signMapper.findUserByClassroomId(sign.getClassroomId());

                // 缓存学生列表
                redisTemplate.opsForValue().set(keys, classUserList,
                        30 + new Random().nextInt(30), TimeUnit.MINUTES);
            }
            // 移除老师
            classUserList.removeIf(user -> user.getId().equals(teacherId));
            // 将这些学生添加到对应的签到列表中
            ret = signMapper.insertSignInStu(signId ,classUserList);

            if (ret != 0) {
                sendToRabbitMQ(classUserList);
            }
        }
        List<SignUser> signUsers = null;
        // 最后返回签到列表
        if (ret != 0) {
            signUsers = signMapper.listUserBySignId(signId,teacherId);
            // 缓存签到列表
            redisTemplate.opsForValue().set(KeyWords.SIGN_IN + "List" + signId, signUsers,
                    20 + new Random().nextInt(20), TimeUnit.MINUTES);
        }
        return signUsers;
    }

    /**
     * 将用户列表发送到rabbitmq
     * @param userList 用户列表 */
    private void sendToRabbitMQ(List<User> userList) {
        for (User user : userList) {
            signInWebSocketHandler.notifyStudent(user.getId());
//            messageSender.sendSignInMessage(user.getId());
        }
    }

    @Override
    public List<SignUser> listUserBySignId(Integer signId) {
        String key = KeyWords.SIGN_IN + "List" + signId;
        List<SignUser> signUserList = (List<SignUser>) redisTemplate.opsForValue().get(key);

        if (signUserList != null) {
            return signUserList;
        }

        signUserList = signMapper.listUserBySignId(signId,null);
        redisTemplate.opsForValue().set(key, signUserList, 1, TimeUnit.MINUTES);
        return signUserList;
    }

    @Override
    public int updateSignInStu(SignUser signUser) {
        return signMapper.updateSignInStu(signUser);
    }
}
